package com.distribute.application.admin;

import com.distribute.framework.core.web.helper.Utils;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

import com.distribute.framework.model.*;

import com.distribute.framework.dao.ISrvArgumentDao;
import com.distribute.framework.dao.ISrvPropertyDao;
import com.distribute.framework.core.lang.ScriptJava;
import com.distribute.framework.core.lang.api;

public class SystemHelper {
	
	/**
	 * 动态属性分类
	 * @author uiu
	 */
	public enum Propertype{;
		private  String name;
		private Propertype(String name){
			this.name = name;
		}
	}
	
	
	private static Integer exps[]={128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216};
	public static Map getSrvLimit(String strFlag,SrvUser srvUser){
		Map map = new HashMap();
		map.put("canView",Utils.getBoolean("canView",false));
		map.put("canAdd",Utils.getBoolean("canAdd",false));
		map.put("canEdit",Utils.getBoolean("canEdit",false));
		map.put("canDel",Utils.getBoolean("canDel",false));
		map.put("canQuery",Utils.getBoolean("canQuery",true));
		map.put("canImport",Utils.getBoolean("canImport",false));
		map.put("canExport",Utils.getBoolean("canExport",false));
		map.put("canStats",Utils.getBoolean("canStats",false));
		map.put("canExp",null);
		map.put("limitValue", 0);
		if(srvUser==null || Utils.empty(strFlag))
			return map;
		if(Utils.getSession("super_user")!=null){
			map.put("canView",Utils.getBoolean("canView",true));
			map.put("canAdd",Utils.getBoolean("canAdd",true));
			map.put("canEdit",Utils.getBoolean("canEdit",true));
			map.put("canDel",Utils.getBoolean("canDel",true));
			map.put("canQuery",Utils.getBoolean("canQuery",true));
			map.put("canImport",Utils.getBoolean("canImport",true));
			map.put("canExport",Utils.getBoolean("canExport",true));
			map.put("canStats",Utils.getBoolean("canStats",true));
			Boolean canExp[]=new Boolean[exps.length];
			Integer limitValue = 1|2|4|8|16|32|64;
			for(int i=0;i<exps.length;i++){
				canExp[i]=(true);
				limitValue|=exps[i];
			}
			map.put("canExp",canExp);
			map.put("limitValue", limitValue);
			return map;
		}
		SrvLimited srvLimited = Utils.getRequest("request."+strFlag);
		if(Utils.getRequest(strFlag)==null){
			srvLimited = SrvLimited.where.slGroup(srvUser.getSuGroup()).slProject$on(SrvProject.where.definex("sp_flag='"+strFlag+"'")).get();
			map.put(strFlag, true);
			map.put("request."+strFlag, srvLimited);
		}
		if(srvLimited==null)
			return map;
		Integer limitValue=srvLimited.getSlLimit();
		if(limitValue!=null){			
			map.put("canView", ((limitValue & 1)==1) && Utils.getBoolean("canView",true));
			map.put("canQuery",Utils.getBoolean("canQuery",true));
			map.put("canAdd", ((limitValue & 2)==2) && Utils.getBoolean("canAdd",true));
			map.put("canEdit", ((limitValue & 4)==4) && Utils.getBoolean("canEdit",true));
			map.put("canDel", ((limitValue & 8)==8) && Utils.getBoolean("canDel",true));
			map.put("canImport", ((limitValue & 16)==16) && Utils.getBoolean("canImport",true));
			map.put("canExport", ((limitValue & 32)==32) && Utils.getBoolean("canExport",true));
			map.put("canStats", ((limitValue & 64)==64) && Utils.getBoolean("canStats",true));
			Boolean canExp[]=new Boolean[exps.length];
			for(int i=0;i<exps.length;i++){
				canExp[i]=(limitValue & exps[i])==exps[i];
			}
			map.put("canExp",canExp);
			map.put("limitValue", limitValue);
		}
		return map;
	}
	/**
	 * 根据用户获取他的权限列表
	 * @param srvUser
	 * @return
	 */
	public static List<SrvLimited> getUserLimiteds(SrvUser srvUser) {
		return SrvLimited.where.slGroup(srvUser.getSuGroup()).list(-1);
	}
	
	
	/**
	 * 根据属性标识获取系统属性列表
	 * @param propertype
	 * @return
	 */
	public static List<SrvProperty> getPropertys(Propertype type){
		return getPropertys(type.name,type.name());
	}
	
	/**
	 * 根据标识获取系统属性
	 * @param flag
	 * @return
	 */
	public static SrvProperty getProperty(String flag){
		ISrvPropertyDao srvPropertyDao = Utils.GetSpringBean("srvPropertyDao");	
		return srvPropertyDao.getSrvProperty(Utils.add(SrvProperty.F.spFlag, flag));
	}
	
	/**
	 * 根据名称、标识获取系统系统属性列表
	 * @param name
	 * @param flag
	 * @return
	 */
	@SuppressWarnings("unused")
	public static List<SrvProperty> getPropertys(String name,String flag){		
		ISrvPropertyDao srvPropertyDao = Utils.GetSpringBean("srvPropertyDao");		
		SrvProperty srvProperty = srvPropertyDao.getSrvProperty(Utils.add(SrvProperty.F.spFlag, flag));
		if(srvProperty==null){//如果不存在则创建一个
			new SrvProperty().spParent(0l).spFlag(flag).spName(name).spStatus((short) 1).save();
			srvProperty = srvPropertyDao.getSrvProperty(Utils.add(SrvProperty.F.spFlag, flag));
		}
		return srvPropertyDao.getSrvPropertyList(Utils.add(SrvProperty.F.spParent, srvProperty.getSpId()),0,-1);
	}
	
	/**
	 * 根据标识获取通用属性
	 * @param flag
	 * @return
	 */
	public static SrvAttribute getAttribute(Long host,String flag){
		return SrvAttribute.where.saHost(host).saFlag(flag).get();
	}
	
	
	
	/**
	 * 根据名称、标识、默认值获取系统参数
	 * @param name
	 * @param flag
	 * @param defaultValue
	 * @return
	 */
	public static <T> T getArgument(String name,String flag,Object defaultValue){
		ISrvArgumentDao srvArgumentDao = Utils.GetSpringBean("srvArgumentDao");
		SrvArgument srvArgument = srvArgumentDao.getSrvArgument(Utils.add("csaFlag", flag));
		if(srvArgument==null)
			srvArgument=srvArgumentDao.saveSrvArgument(
				new SrvArgument().saName(name).saFlag(flag).saValue(defaultValue.toString())
			);
		try {
			if(defaultValue.getClass().getSimpleName().equals("String"))
				return (T)srvArgument.getSaValue();
			else
				return (T) defaultValue.getClass().getMethod("valueOf", String.class).invoke(null, srvArgument.getSaValue());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return (T) defaultValue;
	}
	
	
	/**
	 * 获取查询保存
	 * @param path
	 * @return
	 */
	@SuppressWarnings("unused")
	public static List<SrvQuery> getQuerys(String path) {
		List<SrvQuery> querys = SrvQuery.list(
			Utils.add(SrvQuery.F.sqPath, path)
			.add(SrvQuery.F.sqStatus, 1)
			.add("definex", " (sq_editor="+LoginHelper.getUserId()+" or sq_editor=0)")
		, -1);
		return querys;
	}
	
	
}